#!/bin/bash

source /home/shell/util.sh

requireGit
requireMaven
requireMysqlCient
source /etc/profile.d/java.sh
mkdir -p /var/log/webapps/xxljob/

read -p "是否重置？[no]：" clean_install
clean_install=${clean_install:-"no"};
if [[ "${clean_install}" == "yes" ]]; then
mysql --login-path=dev_host<<EOF
drop database xxl_job;
EOF
fi


cd /usr/local/src
git clone https://gitee.com/xuxueli0323/xxl-job.git

git -C /usr/local/src/xxl-job/ branch -a

read -p "请选择版本[master]：" job_version
job_version=${job_version:-"master"};
git -C /usr/local/src/xxl-job/ checkout -b ${job_version} origin/${job_version}
git -C /usr/local/src/xxl-job/ pull

read -p "数据库地址[${host_addr}]：" mysql_url
mysql_url=${mysql_url:-"${host_addr}"};

read -p "数据库端口[3306]：" mysql_port
mysql_port=${mysql_port:-"3306"};

read -p "数据库用户名[dev_user]：" mysql_username
mysql_username=${mysql_username:-"dev_user"};

read -p "数据库密码[123456]：" mysql_passwd
mysql_passwd=${mysql_passwd:-"123456"};

read -p "WEB服务端口[9004]：" server_port
server_port=${server_port:-"9004"};

expect <<EOF
    spawn mysql_config_editor set --login-path=xxjob_dev_host --host=${mysql_url} --port=${mysql_port} --user=${mysql_username} --password
    expect {
        "password" {send "$mysql_passwd\n";exp_continue}
        "exists" {send "y\n";interact}
    }
EOF

mysql --login-path=xxjob_dev_host<<EOF
create database if not exists xxl_job;
use xxl_job;
source /usr/local/src/xxl-job/doc/db/tables_xxl_job.sql;
EOF

sed -i "s#/data/applogs/xxl-job/xxl-job-admin.log#/var/log/webapps/xxljob/xxl-job-admin.log#g" /usr/local/src/xxl-job/xxl-job-admin/src/main/resources/logback.xml
sed -i "s/server.port=8080/server.port="${server_port}"/g" /usr/local/src/xxl-job/xxl-job-admin/src/main/resources/application.properties
sed -i "s#jdbc:mysql://127.0.0.1:3306/xxl_job#jdbc:mysql://"$mysql_url":"$mysql_port"/xxl_job#g" /usr/local/src/xxl-job/xxl-job-admin/src/main/resources/application.properties
sed -i "s#spring.datasource.username=root#spring.datasource.username="$mysql_username"#g" /usr/local/src/xxl-job/xxl-job-admin/src/main/resources/application.properties
sed -i "s#spring.datasource.password=root_pwd#spring.datasource.password="$mysql_passwd"#g" /usr/local/src/xxl-job/xxl-job-admin/src/main/resources/application.properties

mvn package -f /usr/local/src/xxl-job/xxl-job-admin/pom.xml
mv /usr/local/src/xxl-job/xxl-job-admin/target/xxl-job-admin-*.jar /opt/xxl-job-admin.jar

pid=`jps -l|grep xxl-job|awk '{print $1}'`
if [[ -n "${pid}" ]]; then
    printInfo "杀进程${pid}"
    kill -9 ${pid}
fi


bash /home/shell/serviced-install.sh<<EOF



xxl-job-java
/opt/jdk/bin/java -jar -server -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:+PrintAdaptiveSizePolicy -XX:+UseFastAccessorMethods -XX:+TieredCompilation -Dfile.encoding=utf-8 /opt/xxl-job-admin.jar




EOF

while [ true ]; do
    loadMsg "xxl-job即将完成安装..."
    if [ ! -f "/var/log/webapps/xxljob/xxl-job-admin.log" ]; then
        continue;
    fi
    msg=`tail -10 /var/log/webapps/xxljob/xxl-job-admin.log|grep "init xxl-job admin scheduler success"`;
    if [[ -n "${msg}" ]]; then
        break;
    fi
done

# tail -200 /var/log/webapps/xxljob/xxl-job-admin.log

firewall-addport $server_port
firewall-addport 20770

rm -rf /usr/local/src/xxl-job/

printInfo "======================xxl-job======================"
printInfo "访问地址：http://$host_addr:$server_port/xxl-job-admin"
printInfo "用户名：admin"
printInfo "密码：123456"
printInfo "RPC端口：20770"
printInfo "======================xxl-job======================"